#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;

#endregion

namespace ZO.SmartCore.Windows.Forms.Design
{
    internal class SmartWizardPageDesigner : SmartParentControlDesigner
    {

        #region Constructors
        public SmartWizardPageDesigner()
            : base(propertiesToRemove)
        {
        }

        #endregion

        #region Destructor

        #endregion

        #region Fields
       private DesignerVerbCollection verbs;
        private IComponentChangeService service;
        private IDesignerHost host;
        private static readonly string[] propertiesToRemove = new string[] {
                "Transparent"
            };
        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        /// <summary>
        /// Gets the selection rules that indicate the movement capabilities of a component.
        /// </summary>
        /// <value></value>
        /// <returns>A bitwise combination of <see cref="T:System.Windows.Forms.Design.SelectionRules"></see> values.</returns>
        public override SelectionRules SelectionRules
        {
            get
            {
                return SelectionRules.Locked | SelectionRules.Visible;
            }
        }

   

        /// <summary>
        /// Gets the page.
        /// </summary>
        /// <value>The page.</value>
        private SmartWizardPage Page
        {
            get
            {
                return (this.Control as SmartWizardPage);
            }
        }

        public override DesignerVerbCollection Verbs
        {
            get
            {
                if (this.verbs == null)
                {
                    DesignerVerb verb1 = new DesignerVerb("Add &Page", new EventHandler(this.HandleAddPage));
                    this.verbs = new DesignerVerbCollection(new DesignerVerb[] { verb1 });
                }
                return this.verbs;
            }
        }
 

 
        #endregion

        #region Methods

            /// <summary>
        /// Handles the fit form.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void HandleAddPage(object sender, EventArgs e)
        {
            SmartWizard wizard = this.Page.Wizard;

            SmartWizardPage page1 = this.Page.NextPage;

            DesignerTransaction transaction1 = host.CreateTransaction("Add Wizard Page");
            try
            {
                SmartWizardPage page2 = (SmartWizardPage)host.CreateComponent(typeof(SmartWizardPage));

                page2.PreviousPage = this.Page;
                page2.NextPage = page1;

                TypeDescriptor.GetProperties(this.Page)["NextPage"].SetValue(this.Page, page2);

                if (page1 != null)
                {
                    TypeDescriptor.GetProperties(page1)["PreviousPage"].SetValue(page1, page2);
                }

                service.OnComponentChanging(wizard, TypeDescriptor.GetProperties(wizard)["Pages"]);

                wizard.Pages.Add(page2);

                service.OnComponentChanged(wizard, TypeDescriptor.GetProperties(wizard)["Pages"], null, null);
                (host.GetDesigner(wizard) as SmartWizardDesigner).SelectPage(page2);
            }
            catch
            {
                transaction1.Cancel();
                return;
            }
            finally
            {
                transaction1.Commit();
            }

        }


        /// <summary>
        /// Indicates if this designer's control can be parented by the control of the specified designer.
        /// </summary>
        /// <param name="parentDesigner">The <see cref="T:System.ComponentModel.Design.IDesigner"></see> that manages the control to check.</param>
        /// <returns>
        /// true if the control managed by the specified designer can parent the control managed by this designer; otherwise, false.
        /// </returns>
        public override bool CanBeParentedTo(IDesigner parentDesigner)
        {
            if (parentDesigner != null)
            {
                return (parentDesigner.Component is SmartWizard);
            }
            return false;
        }


        /// <summary>
        /// Initializes the designer with the specified component.
        /// </summary>
        /// <param name="component">The <see cref="T:System.ComponentModel.IComponent"></see> to associate with the designer.</param>
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            this.service = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
            this.host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            this.service.ComponentRemoving += new ComponentEventHandler(service_ComponentRemoving);

        }

        /// <summary>
        /// Handles the ComponentRemoving event of the service control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.ComponentModel.Design.ComponentEventArgs"/> instance containing the event data.</param>
        private void service_ComponentRemoving(object sender, ComponentEventArgs e)
        {
            if (e.Component == Component)
            {
                SmartWizard wizard = this.Page.Wizard;

                SmartWizardPage page1 = this.Page.PreviousPage;
                SmartWizardPage page2 = this.Page.NextPage;

                if (wizard != null)
                {
                    foreach (SmartWizardPage page3 in wizard.GetPagesWithNextPage(this.Page))
                    {
                        TypeDescriptor.GetProperties(page3)["NextPage"].SetValue(page3, page2);
                    }
                    foreach (SmartWizardPage page4 in wizard.GetPagesWithPreviousPage(this.Page))
                    {
                        TypeDescriptor.GetProperties(page4)["PreviousPage"].SetValue(page4, page1);
                    }

                }

            }
        }


        /// <summary>
        /// Raises the PaintAdornments event.
        /// </summary>
        /// <param name="e">The <see cref="System.Windows.Forms.PaintEventArgs"/> instance containing the event data.</param>
        protected override void OnPaintAdornments(PaintEventArgs e)
        {

            using (Pen pen = new Pen(SystemColors.ControlDark))
            {
                pen.DashStyle = DashStyle.Dot;
                Rectangle rectangle = this.Page.ClientRectangle;
                rectangle.Width--;
                rectangle.Height--;
                e.Graphics.DrawRectangle(pen, rectangle);
            }

            base.OnPaintAdornments(e);

        }

 

        #endregion
    }
}
